using System;
using System.Collections.Generic;

namespace WenYu.Core.Parser;

/// <summary>
/// 表达式节点基类
/// </summary>
public abstract class Expression : AstNode
{
}

/// <summary>
/// 整数字面量
/// </summary>
public class IntegerLiteral : Expression
{
    public int Value { get; set; }
    
    public IntegerLiteral(int value)
    {
        Value = value;
    }
    
    public override T Accept<T>(IAstVisitor<T> visitor)
    {
        throw new NotImplementedException();
    }
    
    public override string ToString() => Value.ToString();
}

/// <summary>
/// 实数字面量
/// </summary>
public class DoubleLiteral : Expression
{
    public double Value { get; set; }
    
    public DoubleLiteral(double value)
    {
        Value = value;
    }
    
    public override T Accept<T>(IAstVisitor<T> visitor)
    {
        throw new NotImplementedException();
    }
    
    public override string ToString() => Value.ToString();
}

/// <summary>
/// 字符串字面量
/// </summary>
public class StringLiteral : Expression
{
    public string Value { get; set; }
    
    public StringLiteral(string value)
    {
        Value = value;
    }
    
    public override T Accept<T>(IAstVisitor<T> visitor)
    {
        throw new NotImplementedException();
    }
    
    public override string ToString() => $"\"{Value}\"";
}

/// <summary>
/// 布尔字面量
/// </summary>
public class BooleanLiteral : Expression
{
    public bool Value { get; set; }
    
    public BooleanLiteral(bool value)
    {
        Value = value;
    }
    
    public override T Accept<T>(IAstVisitor<T> visitor)
    {
        throw new NotImplementedException();
    }
    
    public override string ToString() => Value ? "真真" : "假假";
}

/// <summary>
/// 标识符表达式（变量名）
/// </summary>
public class IdentifierExpression : Expression
{
    public string Name { get; set; }
    
    public IdentifierExpression(string name)
    {
        Name = name;
    }
    
    public override T Accept<T>(IAstVisitor<T> visitor)
    {
        throw new NotImplementedException();
    }
    
    public override string ToString() => Name;
}

/// <summary>
/// 二元运算表达式
/// </summary>
public class BinaryExpression : Expression
{
    public Expression Left { get; set; }
    public string Operator { get; set; }
    public Expression Right { get; set; }
    
    public BinaryExpression(Expression left, string op, Expression right)
    {
        Left = left;
        Operator = op;
        Right = right;
    }
    
    public override T Accept<T>(IAstVisitor<T> visitor)
    {
        throw new NotImplementedException();
    }
    
    public override string ToString() => $"({Left} {Operator} {Right})";
}

/// <summary>
/// 一元运算表达式
/// </summary>
public class UnaryExpression : Expression
{
    public string Operator { get; set; }
    public Expression Operand { get; set; }
    public bool IsPrefix { get; set; } // true: ++甲, false: 甲++
    
    public UnaryExpression(string op, Expression operand, bool isPrefix = true)
    {
        Operator = op;
        Operand = operand;
        IsPrefix = isPrefix;
    }
    
    public override T Accept<T>(IAstVisitor<T> visitor)
    {
        throw new NotImplementedException();
    }
    
    public override string ToString() => IsPrefix ? $"{Operator}{Operand}" : $"{Operand}{Operator}";
}

/// <summary>
/// 成员访问表达式（对象.属性）
/// </summary>
public class MemberAccessExpression : Expression
{
    public Expression Object { get; set; }
    public string Member { get; set; }
    
    public MemberAccessExpression(Expression obj, string member)
    {
        Object = obj;
        Member = member;
    }
    
    public override T Accept<T>(IAstVisitor<T> visitor)
    {
        throw new NotImplementedException();
    }
    
    public override string ToString() => $"{Object}的{Member}";
}

/// <summary>
/// 数组访问表达式（数组【索引】）
/// </summary>
public class ArrayAccessExpression : Expression
{
    public Expression Array { get; set; }
    public Expression Index { get; set; }
    
    public ArrayAccessExpression(Expression array, Expression index)
    {
        Array = array;
        Index = index;
    }
    
    public override T Accept<T>(IAstVisitor<T> visitor)
    {
        throw new NotImplementedException();
    }
    
    public override string ToString() => $"{Array}【{Index}】";
}

/// <summary>
/// 函数调用表达式
/// </summary>
public class FunctionCallExpression : Expression
{
    public string Name { get; set; }
    public List<Expression> Arguments { get; set; }
    
    public FunctionCallExpression(string name, List<Expression> arguments)
    {
        Name = name;
        Arguments = arguments;
    }
    
    public override T Accept<T>(IAstVisitor<T> visitor)
    {
        throw new NotImplementedException();
    }
    
    public override string ToString()
    {
        var args = string.Join(", ", Arguments);
        return $"{Name}（{args}）";
    }
}

/// <summary>
/// 数组字面量
/// </summary>
public class ArrayLiteral : Expression
{
    public List<Expression> Elements { get; set; }
    
    public ArrayLiteral(List<Expression> elements)
    {
        Elements = elements;
    }
    
    public override T Accept<T>(IAstVisitor<T> visitor)
    {
        throw new NotImplementedException();
    }
    
    public override string ToString()
    {
        var elements = string.Join(", ", Elements);
        return $"【{elements}】";
    }
}

/// <summary>
/// 类型转换表达式（如：年龄的文字）
/// </summary>
public class TypeConversionExpression : Expression
{
    public Expression Expression { get; set; }
    public string TargetType { get; set; }
    
    public TypeConversionExpression(Expression expression, string targetType)
    {
        Expression = expression;
        TargetType = targetType;
    }
    
    public override T Accept<T>(IAstVisitor<T> visitor)
    {
        throw new NotImplementedException();
    }
    
    public override string ToString() => $"{Expression}的{TargetType}";
}

